package org.example.demo2.Controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
//import com.example.demo.domain.Student;
//import com.example.demo.service.StudentService;
import org.example.demo2.domain.SysOrder;
import org.example.demo2.service.SysOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/Student")
public class DisorderController {
    @Autowired
    private SysOrderService sysOrderService;

    @RequestMapping("/list")
    public String list(Model model) {
        List<SysOrder> students =sysOrderService.list();
        model.addAttribute("students", students);
        return "listStudent";
    }

    //删除学生
    @RequestMapping("remove")
    public String remove(int id) {
        sysOrderService.removeById(id);
        return "redirect:/Student/list/1/3";
    }
    //分页显示
    @RequestMapping("list/{currPage}/{pageSize}")
    public String list(Model model, @PathVariable("currPage") int currPage, @PathVariable("pageSize") int pageSize) {
        if(currPage<1) currPage=1;
        if(pageSize <= 0) pageSize=5;
//        List<Student> students=studentService.findByPage(currPage,pageSize);
        Page<SysOrder> page = new Page<>(currPage,pageSize);
        QueryWrapper<SysOrder> wrapper = new QueryWrapper<>();
        List<SysOrder> students = sysOrderService.page(page, wrapper).getRecords();
        model.addAttribute("students", students);
        return "listStudent";
    }

    @RequestMapping("toPage")
    public String toPage(String page){
        return page;
    }

    //新增
    @RequestMapping("add")
    public String add(SysOrder sysOrder) {
        sysOrderService.save(sysOrder);
        return "redirect:/Student/list/1/3";
    }

    int e=0;
    //准备修改
    @RequestMapping("toEdit")
    public String toEdit(int id, Model model) {
        //alt+enter
        e=id;
        SysOrder oldStudent=sysOrderService.getUserById(id);
        model.addAttribute("student", oldStudent);
        sysOrderService.removeById(id);
        return "editStudent";
    }


    //修改
    @RequestMapping("edit")
    public String edit(SysOrder sysOrder) {
        sysOrder.setId(e);
        sysOrderService.save(sysOrder);
        return "redirect:/Student/list/1/3";
    }

}

